// my_other/dizhi/dizhi.js
let app = getApp()
let yuming = app.globalData.yuming;
const request = require('../../config/request');
Page({

  /**
   * 页面的初始数据
   */
  ...request,
  data: {
    yuming: yuming,
    title: '订单',
    buttonClicked: false,
    addressid: '',
    detail: '',
    spid: '',
    spDetail: '',
  },
  getspXq() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let that = this;
    this.myRequest(yuming + 'shop/scan_pay_goodsinfo', {
      id: that.data.spid,
    }, "POST").then(res => {
      if (res.data.code == 200) {
        wx.hideLoading();
        this.setData({
          spDetail: res.data.data, //商品详情
        })
      }
    })
  },
  getDetail() {
    wx.showLoading({
      title: '加载中',
      icon: 'none'
    })
    let that = this;
    this.myRequest(yuming + 'Hallfood/userAddressInfo', {
        shopId: wx.getStorageSync('shopId'),
      id: that.data.addressid
    }, "POST").then(res => {
      if (res.data.code == 200) {
        wx.hideLoading();
        this.setData({
          detail: res.data.data
        })
      }
    })
  },
  backs() {
    wx.navigateBack({
      delta: 1
    })
  },
  dizhi() {
    wx.navigateTo({
      url: '/my_other/dizhi/dizhi',
    })
  },
  qrdhyz() {
    if (this.data.spid == '') {
      wx.showToast({
        title: '请等待商品加载完成',
        icon: 'none'
      })
    } else if (this.data.addressid == '') {
      wx.showToast({
        title: '请选择收货地址',
        icon: 'none'
      })
    } else {
      this.qrdh();
    }
  },
  qrdh() {
    this.setData({
      buttonClicked: true
    })
    let that = this;
    this.myRequest(yuming + 'shop/scan_shop_pay_submit', {
      shopId: wx.getStorageSync('shopId'),
      goodsId: that.data.spid,
      userAddress: that.data.addressid,
      goods_price: that.data.spDetail.goods_price
    }, "POST").then(res => {
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
      setTimeout(() => {
        that.setData({
          buttonClicked: false
        })
      }, 1500)
      if (res.data.code == 200) {
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 1500)
      } else {
        that.setData({
          buttonClicked: false
        })
      }
    })
  },
  spxz() {
    wx.navigateTo({
      url: '/my_dian/dui/dui',
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      heights: wx.getMenuButtonBoundingClientRect().top + 5
    })
    if (options.id) {
      this.setData({
        spid: options.id
      })
      this.getspXq();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('addressid')) {
      this.setData({
        addressid: wx.getStorageSync('addressid')
      })
      this.getDetail();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
})